---
title: "Use Case"
author: "Kohei Watanabe and Marius Saeltzer"
date: "`r Sys.Date()`"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 6)
require(LSX)

lss <- readRDS("lss_usecase.rds")
dat <- readRDS("data_usecase.rds")

plot_policy <- function(x, by = "class0", sort = FALSE, ...) {
    par(mar = c(10, 4, 3, 1), cex = 0.9, font.main = 1)
    ls <- split(x$lss, factor(x[[by]]))
    n <- lengths(ls)
    m <- sapply(ls, mean, na.rm = TRUE)
    sd <- sapply(ls, sd, na.rm = TRUE)
    se <- sd / sqrt(n)
    ci <- cbind(mean = m, lower = m - se * 1.64, upper = m + se * 1.64, n)
    if (sort)
        ci <- ci[order(m),]
    ci <- cbind(x = seq_len(nrow(ci)), ci)
    matplot(ci[,c(2:4)], type = "n", xaxt = "n", 
            xlim = c(1 -0.5, nrow(ci) + 0.5), ylim = c(-0.9, 0.8),
            ylab = "Temporality", xlab = "", ...)
    points(ci[,1], ci[,2], type = "b", lty = 3)
    arrows(ci[,1], ci[,3], ci[,1], ci[,4], code = 3, angle = 90, length = 0.03)
    text(x = ci[,1], y = par("usr")[3], labels = ci[,5], adj = c(0.5, -1))
    axis(1, ci[,1], rownames(ci), ...)
    #abline(h = mean(m, na.rm = TRUE), col = "black", lty = 2)
}
```

```{r eval=FALSE, include=FALSE}
textplot_terms(lss)
```

```{r}
dat <- subset(dat, !class1 %in% c("Campaign Event", "Political Entity", "Other"))
dat$lss <- rowSums(scale(dat$lss))
table(dat$class1)
nrow(dat)
plot_policy(dat, "class1", las = 3, sort = TRUE)
```

